#!/usr/bin/env bash
set -x

image_registry='registry.cn-hangzhou.aliyuncs.com/xxx'

clear

kubectl get pod -A -o wide | awk "/$(hostname)/ {print \$1\" \"\$2}" > $(hostname)_image_list.txt

while read line
do
  name_space=$(awk '{print $1}' <<< ${line})
  pod_name=$(awk '{print $2}' <<< ${line})

  if [[ ${name_space}x != "kube-system"x ]];then
    image_name=$(kubectl describe pod -n ${name_space} ${pod_name} | awk '/Image:/ {print $NF}' | sort -u)
    for i in ${image_name}
    do
      no_repostirt_name=$(awk -F '/' '{print $NF}' <<< ${i})
      sudo docker tag ${i} ${image_registry}/${no_repostirt_name}
      sudo docker push ${image_registry}/${no_repostirt_name}
      if [ $? -eq 0 ];then
        printf "pull ${image_registry}/${no_repostirt_name} success" | tee -a success.list
      fi
    done
  fi
done < $(hostname)_image_list.txt
